package com.ruoyi.project.formula.chapter.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.formula.chapter.domain.Chapter;
import com.ruoyi.project.formula.chapter.service.IChapterService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 内容章节Controller
 * 
 * @author shixiangwen
 * @date 2020-10-11
 */
@Controller
@RequestMapping("/formula/chapter")
public class ChapterController extends BaseController
{
    private String prefix = "formula/chapter";

    @Autowired
    private IChapterService chapterService;

    @RequiresPermissions("formula:chapter:view")
    @GetMapping()
    public String chapter()
    {
        return prefix + "/chapter";
    }

    /**
     * 查询内容章节列表
     */
    @RequiresPermissions("formula:chapter:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Chapter chapter)
    {
        startPage();
        List<Chapter> list = chapterService.selectChapterList(chapter);
        return getDataTable(list);
    }

    /**
     * 导出内容章节列表
     */
    @RequiresPermissions("formula:chapter:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Chapter chapter)
    {
        List<Chapter> list = chapterService.selectChapterList(chapter);
        ExcelUtil<Chapter> util = new ExcelUtil<Chapter>(Chapter.class);
        return util.exportExcel(list, "chapter");
    }

    /**
     * 新增内容章节
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存内容章节
     */
    @RequiresPermissions("formula:chapter:add")
    @Log(title = "内容章节", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Chapter chapter)
    {
        return toAjax(chapterService.insertChapter(chapter));
    }

    /**
     * 修改内容章节
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Chapter chapter = chapterService.selectChapterById(id);
        mmap.put("chapter", chapter);
        return prefix + "/edit";
    }

    /**
     * 修改保存内容章节
     */
    @RequiresPermissions("formula:chapter:edit")
    @Log(title = "内容章节", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Chapter chapter)
    {
        return toAjax(chapterService.updateChapter(chapter));
    }

    /**
     * 删除内容章节
     */
    @RequiresPermissions("formula:chapter:remove")
    @Log(title = "内容章节", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(chapterService.deleteChapterByIds(ids));
    }
}
